package com.woorigil.social.domain.deal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woorigil.social.Context;
import com.woorigil.social.MenuCategory;
import com.woorigil.social.domain.category.Category;

@Service
public class MainDealService {
	@Autowired DealDao dealDao;
	@Autowired CoupangMainDealDao coupangMainDealDao;
	@Autowired DealService dealService;
	
	public DealViewList findList(DealCondition condition) {
		List<Deal> mainDeals = new ArrayList<Deal>();
		List<MenuCategory> mainMenus = Context.getMainMenus();
		for (MenuCategory mainMenu : mainMenus) {
			if ("쇼핑".equals(mainMenu.getCategory().getName())) {
				for (Integer categoryId : mainMenu.getSubCategoryIds()) {
					condition.setItemCountInPage(2);
					condition.setCategoryIds(Arrays.asList(categoryId));
					mainDeals.addAll(dealDao.search(condition));
				}
			}
		}
		
		return new DealViewList("메인 상품", mainDeals);
	}
	
	public DealViewList findMainDeals(int size) {
		List<MainDealMeta> metas = coupangMainDealDao.findMainDeals(size);
		
		if (Context.getMainDeals() == null || Context.getMainDeals().size() == 0) {
			int count = 0;
			for (MainDealMeta each : metas) {
				Deal deal = dealDao.selectByMetaId(each.getProviderCode(), each.getMetaId());
				if (deal != null && count < size) {
					Context.addMainDeal(deal);
					count++;
				}
			}
		}
		if (Context.getMainDeals() == null || Context.getMainDeals().size() == 0) {
			DealCondition condition = new DealCondition();
			condition.setItemCountInPage(4);
			return dealService.search(condition);
		} else {
			return new DealViewList("메인 상품", Context.getMainDeals());
		}
	}

	public List<Deal> findAllByFirstCategories(String sid, List<Category> firstCategories) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Deal> findAllBySubCategory(String sid, int categoryId) {
		// TODO Auto-generated method stub
		return null;
	}
}
